﻿ USE [master]
GO

/****** Object:  Database [MailQ]    Script Date: 03/11/2010 17:59:37 ******/
CREATE DATABASE [MailQ] ON  PRIMARY 
( NAME = N'MailQ', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10.SQL2008\MSSQL\DATA\MailQ.mdf' , SIZE = 3072KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
 LOG ON 
( NAME = N'MailQ_log', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10.SQL2008\MSSQL\DATA\MailQ_log.ldf' , SIZE = 1024KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)
GO

ALTER DATABASE [MailQ] SET COMPATIBILITY_LEVEL = 100
GO

IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))
begin
EXEC [MailQ].[dbo].[sp_fulltext_database] @action = 'enable'
end
GO

ALTER DATABASE [MailQ] SET ANSI_NULL_DEFAULT OFF 
GO

ALTER DATABASE [MailQ] SET ANSI_NULLS OFF 
GO

ALTER DATABASE [MailQ] SET ANSI_PADDING OFF 
GO

ALTER DATABASE [MailQ] SET ANSI_WARNINGS OFF 
GO

ALTER DATABASE [MailQ] SET ARITHABORT OFF 
GO

ALTER DATABASE [MailQ] SET AUTO_CLOSE OFF 
GO

ALTER DATABASE [MailQ] SET AUTO_CREATE_STATISTICS ON 
GO

ALTER DATABASE [MailQ] SET AUTO_SHRINK OFF 
GO

ALTER DATABASE [MailQ] SET AUTO_UPDATE_STATISTICS ON 
GO

ALTER DATABASE [MailQ] SET CURSOR_CLOSE_ON_COMMIT OFF 
GO

ALTER DATABASE [MailQ] SET CURSOR_DEFAULT  GLOBAL 
GO

ALTER DATABASE [MailQ] SET CONCAT_NULL_YIELDS_NULL OFF 
GO

ALTER DATABASE [MailQ] SET NUMERIC_ROUNDABORT OFF 
GO

ALTER DATABASE [MailQ] SET QUOTED_IDENTIFIER OFF 
GO

ALTER DATABASE [MailQ] SET RECURSIVE_TRIGGERS OFF 
GO

ALTER DATABASE [MailQ] SET  DISABLE_BROKER 
GO

ALTER DATABASE [MailQ] SET AUTO_UPDATE_STATISTICS_ASYNC OFF 
GO

ALTER DATABASE [MailQ] SET DATE_CORRELATION_OPTIMIZATION OFF 
GO

ALTER DATABASE [MailQ] SET TRUSTWORTHY OFF 
GO

ALTER DATABASE [MailQ] SET ALLOW_SNAPSHOT_ISOLATION OFF 
GO

ALTER DATABASE [MailQ] SET PARAMETERIZATION SIMPLE 
GO

ALTER DATABASE [MailQ] SET READ_COMMITTED_SNAPSHOT OFF 
GO

ALTER DATABASE [MailQ] SET HONOR_BROKER_PRIORITY OFF 
GO

ALTER DATABASE [MailQ] SET  READ_WRITE 
GO

ALTER DATABASE [MailQ] SET RECOVERY FULL 
GO

ALTER DATABASE [MailQ] SET  MULTI_USER 
GO

ALTER DATABASE [MailQ] SET PAGE_VERIFY CHECKSUM  
GO

ALTER DATABASE [MailQ] SET DB_CHAINING OFF 
GO


USE [MailQ]
GO
/****** Object:  Table [dbo].[Log]    Script Date: 03/11/2010 18:13:43 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Log](
	[LogID] [bigint] IDENTITY(1,1) NOT NULL,
	[MessageID] [int] NOT NULL,
	[Description] [varchar](max) NULL,
	[TimeStamp] [datetime] NULL,
 CONSTRAINT [PK_Log] PRIMARY KEY CLUSTERED 
(
	[LogID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[QueueState](
	[QueueStateID] [int] IDENTITY(1,1) NOT NULL,
	[Description] [varchar](50) NULL,
 CONSTRAINT [PK_QueueState] PRIMARY KEY CLUSTERED 
(
	[QueueStateID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
SET IDENTITY_INSERT [dbo].[QueueState] ON
INSERT [dbo].[QueueState] ([QueueStateID], [Description]) VALUES (1, N'Unsent')
INSERT [dbo].[QueueState] ([QueueStateID], [Description]) VALUES (2, N'Sending')
INSERT [dbo].[QueueState] ([QueueStateID], [Description]) VALUES (3, N'Sent')
INSERT [dbo].[QueueState] ([QueueStateID], [Description]) VALUES (4, N'Failed')
SET IDENTITY_INSERT [dbo].[QueueState] OFF
/****** Object:  Table [dbo].[Message]    Script Date: 03/11/2010 18:13:43 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Message](
	[MessageID] [int] IDENTITY(1,1) NOT NULL,
	[FromAddress] [varchar](max) NULL,
	[ToAddress] [varchar](max) NULL,
	[CCAddress] [varchar](max) NULL,
	[BCCAddress] [varchar](max) NULL,
	[Subject] [varchar](max) NULL,
	[Body] [nvarchar](max) NULL,
	[IsBodyHTML] [bit] NULL,
	[QueueStateID] [int] NOT NULL,
	[SendAttempts] [int] NULL,
	[LastProcessed] [datetime] NULL,
 CONSTRAINT [PK_Message] PRIMARY KEY CLUSTERED 
(
	[MessageID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[InsertMessage]

@FromAddress	varchar(MAX),
@ToAddress	varchar(MAX),
@CCAddress	varchar(MAX),
@BCCAddress	varchar(MAX),
@Subject	varchar(MAX),
@Body	nvarchar(MAX),
@IsBodyHTML	bit,
@MessageID int out

AS

INSERT INTO Message
(FromAddress, ToAddress, CCAddress,BCCAddress,[Subject],Body,IsBodyHTML, QueueStateID)
VALUES
(@FromAddress, @ToAddress, @CCAddress,@BCCAddress,@Subject,@Body,@IsBodyHTML, 1)

SET @MessageID = SCOPE_IDENTITY()
GO
/****** Object:  ForeignKey [FK_Message_QueueState]    Script Date: 03/11/2010 18:13:43 ******/
ALTER TABLE [dbo].[Message]  WITH CHECK ADD  CONSTRAINT [FK_Message_QueueState] FOREIGN KEY([QueueStateID])
REFERENCES [dbo].[QueueState] ([QueueStateID])
GO
ALTER TABLE [dbo].[Message] CHECK CONSTRAINT [FK_Message_QueueState]
GO
